package cn.com.note.view;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.Calendar;

import cn.com.note.R;

/**
 * Created by sunfulin on 2019/1/16.
 */

public class NotificationBarView extends RelativeLayout {

    Context context;
    TextView tvPower, tvTime;
    ImageView imageBattery;

    public NotificationBarView(Context context) {
        super(context, null);
    }

    public NotificationBarView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        init();
        initBroadcastReceiver();
    }
    BatteryTimeReceiver receiver;
    private void initBroadcastReceiver() {
        //创建广播接受者对象
        receiver=new BatteryTimeReceiver();
        //注册广播接受者java代码
        IntentFilter intentFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
        intentFilter.addAction(Intent.ACTION_TIME_TICK);
        //注册receiver
        context.registerReceiver(receiver, intentFilter);
    }

    private void init() {
        LayoutInflater.from(context).inflate(R.layout.layout_notification_bar, this);
        tvTime = (TextView) findViewById(R.id.tv_time);
        tvPower = (TextView) findViewById(R.id.tv_power_quantity);
        imageBattery = (ImageView) findViewById(R.id.imageBattery);
        updateTime();
    }
    /**
     * 设置电池图标
     *
     * @param level
     *            0-10
     */
    private void setBatteryImageByLevel(int level) {
        int drawableID=R.mipmap.battery_100;
        if(level ==100){
            drawableID=R.mipmap.battery_100;
        }else if(level >80){
            drawableID=R.mipmap.battery_80_90;
        }else if(level >60){
            drawableID=R.mipmap.battery_60_70;
        }else if(level >40){
            drawableID=R.mipmap.battery_40_60;
        }else if(level >15){
            drawableID=R.mipmap.battery_20_40;
        }else {
            drawableID=R.mipmap.battery_low;
        }
        imageBattery.setImageResource(drawableID);
    }

    private void updateBattery(){
        IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
        Intent receiver = context.registerReceiver(null, filter);
        int level = receiver.getIntExtra("level", 0);//获取当前电量
        int scale = receiver.getIntExtra("scale", 0);//获取总电量
        int status = receiver.getIntExtra("status", 0);//获取充电状态
        int voltage = receiver.getIntExtra("voltage", 0);//获取电压(mv)
        int temperature = receiver.getIntExtra("temperature", 0);//获取温度(数值)
        double t = temperature / 10.0;  //运算转换,电池摄氏温度，默认获取的非摄氏温度值
        //把它转成百分比
        tvPower.setText(((level * 100) / scale) + "%");
        setBatteryImageByLevel(level);
    }
    private void updateTime(){
        long time = System.currentTimeMillis();
        Calendar mCalendar = Calendar.getInstance();
        mCalendar.setTimeInMillis(time);
        int hour = mCalendar.get(Calendar.HOUR);
        int minute = mCalendar.get(Calendar.MINUTE);
        int apm = mCalendar.get(Calendar.AM_PM);
        String str=apm==0?"AM":"PM";
        tvTime.setText(""+hour+":"+minute+" "+str);
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        context.unregisterReceiver(receiver);
    }
    /**
     * 广播接受者
     */
    class BatteryTimeReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub
            //判断它是否是为电量变化的Broadcast Action
            if (Intent.ACTION_BATTERY_CHANGED.equals(intent.getAction())) {
                //获取当前电量
                int level = intent.getIntExtra("level", 0);
                //电量的总刻度
                int scale = intent.getIntExtra("scale", 100);
                int status = intent.getIntExtra("status", 0);
                //把它转成百分比
                tvPower.setText(((level * 100) / scale) + "%");
                setBatteryImageByLevel(level);
            }else if(Intent.ACTION_TIME_TICK.equals(intent.getAction())){//时间变化
                updateTime();
            }
        }

    }
}
